package com.dhcc.bpm.common.utils;


import com.dhcc.bpm.modules.mail.service.MailService;
import com.dhcc.bpm.modules.mail.service.SendEasternMsgService;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;


/**
 * @ClassName EmailUtils
 * @Description 发送邮件工具类
 * @Author xiafayu
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@Component
@AllArgsConstructor
@NoArgsConstructor
public class EmailUtils
{
    private static final Logger log = LoggerFactory.getLogger(EmailUtils.class);

    @Autowired
    private MailService mailService;

    @Autowired
    SendEasternMsgService easternMsgService;

    @Autowired
    ResourceLoader resourceLoader;

    @Value("${spring.mail.mailType}")
    private String mailType;
    @Value("${bpm.approval.isSendMail}")
    private boolean isSendMail;


    /**
     * * 发送邮件--管理端发送
     *
     * @param title 邮件标题
     * @param emailAddr 目标邮箱地址
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return true：发送成功 false：发送失败
     */
    public boolean sendEmail(String title, String emailAddr, String subject, String content) {
        if(isSendMail){
            try {
                if ("local".equals(mailType)) {
                    Resource resource = resourceLoader.getResource("classpath:mail/template_v1.html");
                    InputStream is = resource.getInputStream();
                    String content1 = WebUtils.tranferFileToStr(is);
                    content = content1.replace("email_content", content);
                    mailService.sendHtmlMail(emailAddr, subject, content);
                }
                else {
                    List<String> userList = new LinkedList<>();
                    userList.add(emailAddr);
                    easternMsgService.sendMail(title, content, userList);
                }
            }catch (Exception e){
                log.error("邮件发送异常：{}", e);
                return false;
            }
        }

        return  true;
    }

    public boolean sendConfirmEmail(String title, String emailAddr, String subject, String content,String emailUrl) {
        if(isSendMail){
            try {
                if ("local".equals(mailType)) {
                    Resource resource = resourceLoader.getResource("classpath:mail/template_v4.html");
                    InputStream is = resource.getInputStream();
                    String content1 = WebUtils.tranferFileToStr(is);
                    content = content1.replace("email_content", content);
                    emailUrl = content1.replace("email_url", emailUrl);
                    mailService.sendHtmlMail(emailAddr, subject, content,emailUrl);
                } else {
                    List<String> userList = new LinkedList<>();
                    userList.add(emailAddr);
                    easternMsgService.sendMail(title, content, userList);
                }
            }catch (Exception e){
                log.error("邮件发送异常：{}", e);
                return false;
            }
        }

        return  true;
    }

}
